package cn.zhangmh.easymvp;

import android.content.Context;

import com.google.gson.GsonBuilder;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import cn.zhangmh.easymvp.books.BooksApi;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zmh on 2017/4/30.
 */

public class RetrofitHelper {

    private Context context;

    OkHttpClient client = new OkHttpClient();

    GsonConverterFactory factory =  GsonConverterFactory.create(new GsonBuilder().create());

    private  static  RetrofitHelper  instance = null;

    private Retrofit mRetroft = null;

    public  static  RetrofitHelper  getInstance(Context context){
        if (instance == null){
            instance = new RetrofitHelper(context);
        }
        return  instance;
    }

    private  RetrofitHelper(Context mcontext){
        context = mcontext;
        init();
    }

    private  void  init(){
        restApp();
    }

    private  void  restApp(){
        mRetroft = new Retrofit.Builder()
                .baseUrl("https://api.douban.com/v2/")
                .client(client)
                .addConverterFactory(factory)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    public BooksApi getServer(){
        return  mRetroft.create(BooksApi.class);
    }
}
